package main

import (
	"github.com/streadway/amqp"
	"log"
	"os"
)

func main() {
	//connect
	conn, err := amqp.Dial("amqp://guest:guest@localhost:5672/")
	if err != nil {
	}
	defer conn.Close()

	//channels
	channel, err := conn.Channel()
	if err != nil {
	}
	defer channel.Close()
	// exchange
	channel.ExchangeDeclare(
		"logs_direct",
		"direct",
		true,
		false,
		false,
		false,
		nil,
	)
	//QueueDeclare
	queue, err := channel.QueueDeclare("", // name
		false, // durable
		false, // delete when unused
		true,  // exclusive
		false, // no-wait
		nil,   // arguments
	)
	if len(os.Args) < 2 {
		log.Printf("Usage: %s [info] [warning] [error]", os.Args[0])
		os.Exit(0)
	}
	//binding
	for _, s := range os.Args[1:] {
		log.Printf("Binding queue %s to exchange %s with routing key %s",
			queue.Name, "logs_direct", s)
		channel.QueueBind(queue.Name, s, "logs_direct", false, nil)
	}

	msgs, err := channel.Consume(queue.Name, "", true, false, false, false, nil)
	forever := make(chan bool)
	go func() {
		for msg := range msgs {
			log.Printf(" [x] %s", msg.Body)
		}
	}()

	<-forever
}
